#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys

'''
对聚好类的文件数据进行替换，使数据具有原始的学生信息，便于人工观察
'''

def main(argv):
    transform_directory = "/Users/David/Desktop/transform"

    origin_file = "/Users/David/Desktop/origin.csv"
    origin_data = []
    with open(origin_file, 'r', encoding='utf-8') as f:
        line = f.readline()
        while line:
            origin_data.append(line)
            line = f.readline()
    print('原始数据：', origin_data)

    files = os.listdir(transform_directory)
    for file in files:
        if file != '.DS_Store':
            transform_file = os.path.join(transform_directory, file)
            transform(transform_file, origin_data)


def transform(transform_file, origin_data):
    result_data = []
    transform_data = []
    print(transform_file)
    with open(transform_file, 'r', encoding='utf-8') as f:
        line = f.readline()
        while line:
            transform_data.append(line)
            line = f.readline()
    print('待转换的数据：', transform_data)

    for line in transform_data:
        if line == 'vital_capacity,m50_run,sit_and_reach,jump_rope,sit_up,m50_return_run,\n':
            line = 'id,学校,班级,姓名,性别,身高,体重,BMI,vital_capacity,m50_run,sit_and_reach,jump_rope,sit_up,m50_return_run\n'
        flag = False
        for data in origin_data:
            if line == '\n':
                break
            if line.strip() in data.strip():
                result_data.append(data)
                flag = True
                break
        if not flag:
            result_data.append(line)

    print('结果数据：', result_data)

    result_file = transform_file.replace('.csv', '_transform.csv')

    with open(result_file, 'w', encoding='utf-8') as f:
        for result_line in result_data:
            f.write(result_line)


if __name__ == '__main__':
    argv = sys.argv
    main(argv)
